define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'editable','layui'], function ($, undefined, Backend, Table, Form,undefined,layui) {

    var Controller = {
        index: function () {
            
            layui.use(['tree','util'], function () {
                console.log('*******************454545')
                
                var tree = layui.tree;
                console.log(tree)
//                var layer = layui.layer;
                var util = layui.util;
//                var data = [{title:'A-1',id:1,field:'name1',checked:true,spread:true,children:[{title:'B-1-1 可允许跳转',id:3,field:'name11',href:'',children:[{title:'C-1-1-3',id:23,field:'',children:[{title:'D-1-1-3-1',id:24,field:'',children:[{title:'E-1-1-3-1-1',id:30,field:''},{title:'E-1-1-3-1-2',id:31,field:''}]}]},{title:'C-1-1-1',id:7,field:'',children:[{title:'D-1-1-1-1 可允许跳转',id:15,field:'',href:''}]},{title:'C-1-1-2',id:8,field:'',children:[{title:'D-1-1-2-1',id:32,field:''}]}]},{title:'B-1-2',id:4,spread:true,children:[{title:'C-1-2-1',id:9,field:'',disabled:true},{title:'C-1-2-2',id:10,field:''}]},{title:'B-1-3',id:20,field:'',children:[{title:'C-1-3-1',id:21,field:''},{title:'C-1-3-2',id:22,field:''}]}]},{title:'A-2',id:2,field:'',spread:true,children:[{title:'B-2-1',id:5,field:'',spread:true,children:[{title:'C-2-1-1',id:11,field:''},{title:'C-2-1-2',id:12,field:''}]},{title:'B-2-2',id:6,field:'',children:[{title:'C-2-2-1',id:13,field:''},{title:'C-2-2-2',id:14,field:'',disabled:true}]}]},{title:'A-3',id:16,field:'',children:[{title:'B-3-1',id:17,field:'',fixed:true,children:[{title:'C-3-1-1',id:18,field:''},{title:'C-3-1-2',id:19,field:''}]},{title:'B-3-2',id:27,field:'',children:[{title:'C-3-2-1',id:28,field:''},{title:'C-3-2-2',id:29,field:''}]}]}];
                tree.render({
                    elem: '#ID-tree-demo',
                    data: Config.departmentlist,
                    customName: { // 自定义 data 字段名 --- 2.8.14+
                        id: 'id',
                        title: 'name',
                        children: 'childlist'
                    },
                    showLine:true,
                    showCheckbox: true, // 是否显示复选框
//                    onlyIconControl: true, // 是否仅允许节点左侧图标控制展开收缩
                    id: 'demo-id-1',
                    click: function (obj) {
                        var data = obj.data;  //获取当前点击的节点数据
                        layer.msg('状态：' + obj.state + '<br>节点数据：' + JSON.stringify(data));
                    },
                    oncheck: function(obj){
                        console.log(obj.data); // 得到当前点击的节点数据
                        console.log(obj.checked); // 节点是否被选中
                        console.log(obj.elem); // 得到当前节点元素
                    }
                });

                util.on({
                    e1: function(){
                      console.log(this); // 当前触发事件的 DOM 元素
                      layer.msg('触发了事件 1');
                    },
                    e2: function(){
                      layer.msg('触发了事件 2');
                    }
                });


            });
            
            
            
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'zhongzhuan/fenpei/assignkehu/index' + location.search,
//                    add_url: 'zhongzhuan/fenpei/assignkehu/add',
                    edit_url: 'zhongzhuan/fenpei/assignkehu/edit',
//                    del_url: 'zhongzhuan/fenpei/assignkehu/del',
                    multi_url: 'zhongzhuan/fenpei/assignkehu/multi',
//                    import_url: 'zhongzhuan/fenpei/assignkehu/import',
                    table: 'ec_assign_kehu',
                }
            });

            var table = $("#table");
            
            table.on('post-common-search.bs.table', function (event, table) {

                // 以下2行代码是关键，是加载完表格后，把销售搜索空实例化成selectpage格式
                var form = $("form",table.$commonsearch);
                $("input[name='deptId']",form).addClass("selectpage").data("source","zhongzhuan/ec/deparment/index").data("primaryKey","deptId").data("field","deptName");
                                
                
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'people',
                sortOrder:'desc',
                columns: [
                    [
//                        {checkbox: true},
//                        {field: 'id', title: __('Id'),searchable:false},
                        {field: 'user_id', title:'员工', operate: 'LIKE',searchable:false,formatter: function(value, row, index) {
                                return row.name + "[" + row.mobile +"]";
                                
                                
                            }
                        },
                        {field: 'name', title:'员工', operate: 'LIKE',visible:false,searchable:true},
                        {field: 'deptId', title: '部门', operate: 'LIKE',visible:false},
                        {field: 'deptName', title: '部门', operate: 'LIKE',searchable:false},
//                        {field: 'deptId', title: '部门', operate: 'LIKE',visible:false},
                        {field: 'people', title: '计划分配数', operate: 'LIKE',searchable:false,editable: true,sortable:true},
                        {field: 'success_people', title: '成功分配数', operate: 'LIKE',searchable:false,sortable:true},
                        {field: 'is_shangban', title:'上班状态', searchList: {"0":'未上班',"1":'上班中'},sortable:true, formatter: Table.api.formatter.toggle},
//                        {field: 'auto_assign', title: '自动分配', searchList: {"0":__('auto_assign 0'),"1":__('auto_assign 1')}, formatter: Table.api.formatter.status},
//                        {field: 'auto_assign', title: '自动分配', searchList: {"0":__('auto_assign 0'),"1":__('auto_assign 1')}, formatter: Table.api.formatter.toggle},
                        {field: 'assign_status', title: '分配状态', searchable:false,searchList: {"0":'未完成',"1":'已完成'},sortable:true, formatter: Table.api.formatter.status},
//                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
//                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter:Table.api.formatter.operate, }
                    ]
                ]
            });
            
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
